package com.example.exam.post.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.db.PageInfoBean;
import com.example.exam.post.model.PostBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class PostDao extends BaseDao<PostBean, Integer> {
    @Override
        /**
            * @Description:添加岗位SQL语句
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getAddSql() {
        return "INSERT INTO staff_post(NAME,descr,grade) VALUES(:name,:descr,:grade)";
    }
    @Override
        /**
            * @Description:删除岗位信息sql语句
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getDeleteByIdSql(){
        return "DELETE FROM staff_post WHERE id=?";
    }
    @Override
        /**
            * @Description:查询一条岗位信息SQL语句
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String findByIdSql() {
        return "SELECT * FROM staff_post WHERE id=?";
    }
    /**
     * @Description:修改
     * @Param: [postBean]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer update(PostBean postBean){

        String sql="UPDATE staff_post SET NAME=?,descr=?,grade=? WHERE id=?";
        List<Object> list=new ArrayList<Object>();
        list.add(postBean.getName());
        list.add(postBean.getDescr());
        list.add(postBean.getGrade());
        list.add(postBean.getId());
        return getJtl().update(sql,list.toArray());
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getUpdateAllFieldSql(){
        return "UPDATE staff_post SET NAME=:name,descr=:descr,grade=:grade WHERE id=:id";
    }
    /**
     * @Description:查询岗位列表信息
     * @Param: [pageInfoBean, name]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public PageInfoBean list(PageInfoBean pageInfoBean,String name){

        try{
            Map<String, Object> paramMap = new HashMap<String, Object>();
            String sql="SELECT * FROM staff_post";
            if (StringUtils.isNotEmpty(name)){
                sql += " WHERE NAME LIKE '%"+name+"%'";
            }
            return pageForBean(sql,paramMap,pageInfoBean);
        }catch (Exception e){
            e.fillInStackTrace();
            return null;
        }
    }/**
     * @Description:查询岗位编号和姓名作为员工信息岗位的下拉框数据
     * @Param: []
     * @return: java.util.List<com.example.exam.post.model.PostBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<PostBean> listPost(){
        try{
            String sql="select id,name from staff_post";
            return getJtl().query(sql,new BeanPropertyRowMapper<PostBean>(PostBean.class));
        }catch (Exception e){
            e.fillInStackTrace();
            return null;
        }
    }
}
